/*********************************************************************************
 * SugarCRM is a customer relationship management program developed by
 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License along with
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * 
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by SugarCRM".
 ********************************************************************************/

<!-- Dependency -->
<script src="http://yui.yahooapis.com/2.6.0/build/yahoo/yahoo-min.js"></script>
<!-- Used for Custom Events and event listener bindings -->
<script src="http://yui.yahooapis.com/2.6.0/build/event/event-min.js"></script>
<!-- Source file -->
<script src="http://yui.yahooapis.com/2.6.0/build/connection/connection-min.js"></script>
<script src="http://yui.yahooapis.com/2.6.0/build/json/json-min.js"></script>
<script>
var SugarRest = function(){}

SugarRest.callCount = -1;
SugarRest.startTest = function(name, password){
	SugarRest.callCount = 1;
	SugarRest.login(name, password, 'JAVASCRIPT TEST');
}
SugarRest.test = function(o){
	data = SugarRest.logResponse(o);
	switch(SugarRest.callCount){
		case 1:
			SugarRest.callCount++;
			SugarRest.getServerInfo();
			SugarRest.session = data['id'];
			break;
		case  2:
			SugarRest.callCount++;
			SugarRest.getUserId();
			break;
		case  3:
			SugarRest.callCount++;
			SugarRest.getEntry('Contacts', '17700cb1-9da0-f511-f9d7-490b635b9ead');
			break;
		case  4:
			SugarRest.callCount++;
			SugarRest.getEntryList('Contacts');
			break;
		case  5:
			SugarRest.callCount++;
			SugarRest.getModuleFields('Contacts', []);
			break;
		case  6:
			SugarRest.callCount++;
			SugarRest.getEntries('Contacts', ['17700cb1-9da0-f511-f9d7-490b635b9ead', '177cf935-e488-04c3-7af3-490b632b4ab0']);
			break;
		case  7:
			SugarRest.callCount++;
			SugarRest.setRelationship('Accounts', 'mastd368-c7er-cof8-16pu-47d57b16pets', 'contacts', ['2f043319-7cb0-6bfa-75d3-490b63fa6c08', 'c57d7598-42e6-cca8-a302-490b63118b48']);
			break;
		case  8:
			SugarRest.callCount++;
			SugarRest.setRelationships(['Accounts', 'Contacts'], ['mastd368-c7er-cof8-16pu-47d57b16pets', '2f043319-7cb0-6bfa-75d3-490b63fa6c08'], ['contacts', 'opportunities'], [['2f043319-7cb0-6bfa-75d3-490b63fa6c08'], ['52acdc25-6a6e-a574-6936-490b63fbc1df']]);
			break;
		case  9:
			SugarRest.callCount++;
			SugarRest.getRelationships('Accounts', 'mastd368-c7er-cof8-16pu-47d57b16pets', 'contacts', '', ['first_name', 'last_name', 'primary_address_city'], [{'name' : 'opportunities', 'value' : ['name', 'type', 'lead_source']}, {'name' : 'email_addresses', 'value' : ['id', 'email_address', 'opt_out', 'primary_address']}], 0);
			break;
		case  10:
			SugarRest.callCount++;
			SugarRest.setEntry('Accounts', [{'name' : 'city', 'value' : 'SFO'},
										   {'name' : 'name', 'value' : 'DCE Puppet Master111'}]);
			break;
		case  11:
			SugarRest.callCount++;
			SugarRest.setEntries('Accounts', [[{'name' : 'city', 'value' : 'SFO'},
										   {'name' : 'name', 'value' : 'Acc1'}], [{'name' : 'city', 'value' : 'SFO'},
										   {'name' : 'name', 'value' : 'Acc2'}]]);
			break;
		case  12:
			SugarRest.callCount++;
			SugarRest.seamlessLogin();
			break;
		case  13:
			SugarRest.callCount++;
			SugarRest.setNoteAttachment({'id' : 'bb79c0f3-78e1-c6fc-2c41-490b636653c0', 'related_module_id' : '3d921d14-047f-cc7d-1988-490b63592ae8', 'related_module_name' : 'Accounts'});
			break;
		case  14:
			SugarRest.callCount++;
			SugarRest.getNoteAttachment('bb79c0f3-78e1-c6fc-2c41-490b636653c0');
			break;
		case  15:
			SugarRest.callCount++;
			SugarRest.getDocumentRevision('ab18578e-17f5-5fc0-96ba-4925a9baaa20');
			break;
		case  16:
			SugarRest.callCount++;
			SugarRest.search('admin', '21232f297a57a5a743894a0e4a801fc3', 'a', ['Accounts', 'Contacts'], 0, 5);
			break;
			/*
		case  17:
			SugarRest.callCount++;
			SugarRest.setDocumentRevision({'id' : '8d7f0917-19d3-9f88-80d7-49121ddb62e2','document_name' : 'test doc',
																					   		 'revision' : 'D',
																					   		 'filename' : 'code1.txt',
																					   		 'file' : 'JHFzID0gJyc7DQppZiAoaXNzZXQoJF9TRVJWRVJbJ1FVRVJZX1NUUklORyddKSkgew0KCSRxcyA9ICRfU0VSVkVSWydRVUVSWV9TVFJJTkcnXTsNCn0gZWxzZWlmIChpc3NldCgkSFRUUF9TRVJWRVJfVkFSU1snUVVFUllfU1RSSU5HJ10pKSB7DQoJJHFzID0gJEhUVFBfU0VSVkVSX1ZBUlNbJ1FVRVJZX1NUUklORyddOw0KfSBlbHNlIHsNCgkkcXMgPSAnJzsNCn0NCg0KJHQxID0gc3RydG90aW1lKCJub3ciKTsNCg0KaWYgKGVyZWcoJ3dzZGwnLCAkcXMpICl7DQoJZ2xvYmFsICRzdWdhcl9jb25maWc7D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vzc2lvbiddKSkgew0KCQkJCSRHTE9CQUxTWydsb2cnXS0+ZGVidWcoIkluIHNlcnZpY2UsIGRhdGEgPSAiIC4gJHRoaXMtPm1ldGhvZHBhcmFtc1snc2Vzc2lvbiddKTsNCgkJCX0NCg=='});
			break;
			*/
	}

}

console = false;
if(!console){
	console = {};
	console.log = function(){

	}
}

SugarRest.getRequestData = function(method_name, data) {
	var returndata = 'method=' + method_name + '&input_type=json&response_type=json';
	if (data != '') {
		returndata = returndata + '&rest_data=' + data;
	}
	return returndata
}
SugarRest.logResponse = function(o){

	data = YAHOO.lang.JSON.parse(o.responseText);
	console.log(data);
	return data;
}

SugarRest.getServerInfo =  function(){
	console.log('Getting Server Info');
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php' , {success:SugarRest.test, failure:SugarRest.test}, SugarRest.getRequestData('get_server_info'));
}

SugarRest.getModuleFields =  function(module, fields){
	console.log('Getting Server Info');
	var data = [SugarRest.session, module, fields];
	m = YAHOO.lang.JSON.stringify(data);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php' , {success:SugarRest.test, failure:SugarRest.test}, SugarRest.getRequestData('get_module_fields', m));
}

SugarRest.login =  function(name, password, application){
	console.log('Logging In');
	var loginData = [{
		user_name: 'admin',
		password: '21232f297a57a5a743894a0e4a801fc3'
	},'javascriptTest'];
	m = YAHOO.lang.JSON.stringify(loginData);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php' , {success:SugarRest.test, failure:SugarRest.test}, SugarRest.getRequestData('login', m));
}

SugarRest.getUserId =  function(){
	console.log('Getting User Id ');
	m = YAHOO.lang.JSON.stringify(SugarRest.session);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php' , {success:SugarRest.test, failure:SugarRest.test}, SugarRest.getRequestData('get_user_id', m));
}


SugarRest.getEntryList = function(module){
	var data = [SugarRest.session, module, "", '', 0, [], [{
		'name': 'email_addresses',
		'value': ['id', 'email_address', 'opt_out', 'primary_address',]
	}], 10, 0];
	m = YAHOO.lang.JSON.stringify(data);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php', {
		success: SugarRest.test,
		failure: SugarRest.test
	}, SugarRest.getRequestData('get_entry_list', m));
}


SugarRest.getEntry = function(module, id){
	var data = [SugarRest.session, module, id, ['last_name', 'email1', 'date_modified','description'], [{
		'name': 'email_addresses',
		'value': ['id', 'email_address', 'opt_out', 'primary_address',]
	}]];
	m = YAHOO.lang.JSON.stringify(data);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php', {
		success: SugarRest.test,
		failure: SugarRest.test
	}, SugarRest.getRequestData('get_entry', m));
}

SugarRest.getEntries = function(module, ids){
	var data = [SugarRest.session, module, ids, ['last_name', 'email1', 'date_modified','description'], [{
		'name': 'email_addresses',
		'value': ['id', 'email_address', 'opt_out', 'primary_address',]
	}]];
	m = YAHOO.lang.JSON.stringify(data);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php', {
		success: SugarRest.test,
		failure: SugarRest.test
	}, SugarRest.getRequestData('get_entries', m));
}

SugarRest.setRelationship = function(module, module_id, link_field_name, related_ids){
	var data = [SugarRest.session, module, module_id, link_field_name, related_ids];
	m = YAHOO.lang.JSON.stringify(data);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php', {
		success: SugarRest.test,
		failure: SugarRest.test
	}, SugarRest.getRequestData('set_relationship', m));
}

SugarRest.setRelationships = function(modules, module_ids, link_field_names, related_ids){
	var data = [SugarRest.session, modules, module_ids, link_field_names, related_ids];
	m = YAHOO.lang.JSON.stringify(data);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php', {
		success: SugarRest.test,
		failure: SugarRest.test
	}, SugarRest.getRequestData('set_relationships', m));
}

SugarRest.getRelationships = function(module, module_id, link_field_name, related_module_query, related_fields, related_module_link_name_to_fields_array, deleted){
	var data = [SugarRest.session, module, module_id, link_field_name, related_module_query, related_fields, related_module_link_name_to_fields_array, deleted];
	m = YAHOO.lang.JSON.stringify(data);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php', {
		success: SugarRest.test,
		failure: SugarRest.test
	}, SugarRest.getRequestData('get_relationships', m));
}

SugarRest.setEntry = function(module, name_value_list){
	var data = [SugarRest.session, module, name_value_list];
	m = YAHOO.lang.JSON.stringify(data);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php', {
		success: SugarRest.test,
		failure: SugarRest.test
	}, SugarRest.getRequestData('set_entry', m));
}

SugarRest.setEntries = function(module, name_value_lists){
	var data = [SugarRest.session, module, name_value_lists];
	m = YAHOO.lang.JSON.stringify(data);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php', {
		success: SugarRest.test,
		failure: SugarRest.test
	}, SugarRest.getRequestData('set_entries', m));
}

SugarRest.seamlessLogin = function(){
	var data = [SugarRest.session];
	m = YAHOO.lang.JSON.stringify(data);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php', {
		success: SugarRest.test,
		failure: SugarRest.test
	}, SugarRest.getRequestData('seamless_login', m));
}

SugarRest.setNoteAttachment = function(noteObject){
	var data = [SugarRest.session, noteObject];
	m = YAHOO.lang.JSON.stringify(data);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php', {
		success: SugarRest.test,
		failure: SugarRest.test
	}, SugarRest.getRequestData('set_note_attachment', m));
}

SugarRest.getNoteAttachment = function(noteId){
	var data = [SugarRest.session, noteId];
	m = YAHOO.lang.JSON.stringify(data);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php', {
		success: SugarRest.test,
		failure: SugarRest.test
	}, SugarRest.getRequestData('get_note_attachment', m));
}

SugarRest.setDocumentRevision = function(revisionObject){
	var data = [SugarRest.session, revisionObject];
	m = YAHOO.lang.JSON.stringify(data);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php', {
		success: SugarRest.test,
		failure: SugarRest.test
	}, SugarRest.getRequestData('set_document_revision', m));
}

SugarRest.getDocumentRevision = function(docRevId){
	var data = [SugarRest.session, docRevId];
	m = YAHOO.lang.JSON.stringify(data);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php', {
		success: SugarRest.test,
		failure: SugarRest.test
	}, SugarRest.getRequestData('get_document_revision', m));
}

SugarRest.search = function(username, password, search_string, modules_to_search, offset, maxresults){
	var data = [username, password, search_string, modules_to_search, offset, maxresults];
	m = YAHOO.lang.JSON.stringify(data);
	YAHOO.util.Connect.asyncRequest('POST', 'v2/rest.php', {
		success: SugarRest.test,
		failure: SugarRest.test
	}, SugarRest.getRequestData('search_by_module', m));
}
SugarRest.startTest();
</script>


